﻿using System;
using System.Data;
using System.Linq;
using System.Data.Entity;
using System.Web.Mvc;
using System.Web.Security;
using Lbb.URating.DL;
using Lbb.Web.Mvc;

namespace Lbb.URating.Controllers
{
    /// <summary>
    /// 用户评分控制器
    /// </summary>
    /// <author>Chiron</author>
    public class U_RatingController : URatingControllerBase
    {
        public ActionResult Rating(Guid catId, string objId, string hash, decimal[] ratings)
        {
            var result = new JsonResponse();

            try
            {
                #region 条件校验
                if (string.IsNullOrWhiteSpace(objId) || string.IsNullOrWhiteSpace(hash) || ratings == null) throw new Exception("参数不正确!");

                var user = Membership.GetUser();
                if (user == null) throw new Exception("请先登录!");

                //var hUser = new MemberUser(user);
                var uId = (Guid)user.ProviderUserKey;
                var cat = U_ClickCategoryRepository.Get(catId);
                if (cat == null) throw new Exception("评分分类已不存在!");

                var clickCount = cat.U_Click.Where(d => d.IsRating).Count();
                if (clickCount == 0 || clickCount != ratings.Length) throw new Exception("评分项个数不正确!");
                if (hash != cat.ClicksHash) throw new Exception("评分项有变动,请刷新页面后再次尝试!");

                var isRated = cat.U_Click.Where(d => d.IsRating).Count(d => d.U_ClickRating.Count(e => e.ObjectID == objId && e.U_Rating.Count(f => f.UserID == uId) > 0) > 0) == clickCount;
                if (isRated && !cat.AllowRedo) throw new Exception("您已评过分了!");
                #endregion 条件校验

                #region 更新用户评分
                var clicks = cat.U_Click.Where(d => d.IsRating).OrderBy(d => d.Ordering).ToList();
                for (var i = 0; i < ratings.Length; i++)
                {
                    var click = clicks[i];
                    var rating = ratings[i];

                    var cRating = click.U_ClickRating.FirstOrDefault(d => d.ObjectID == objId);
                    if (cRating == null)
                    {
                        cRating = new U_ClickRating
                        {
                            U_Click = click,
                            ObjectID = objId
                        };
                        U_ClickRatingRepository.Add(cRating);
                        U_ClickRatingRepository.Save();
                    }

                    var myRating = click.U_ClickRating.FirstOrDefault(d => d.ObjectID == objId).U_Rating.FirstOrDefault(d => d.UserID == uId);
                    if (myRating == null)
                    {
                        myRating = new U_Rating
                        {
                            Created = DateTime.Now,
                            LastUpdated = DateTime.Now,
                            UserID = uId,
                            Username = user.UserName,
                            U_ClickRating = cRating
                        };
                        U_RatingRepository.Add(myRating);
                        U_RatingRepository.Save();
                        cRating.ClickCount += 1;
                    }
                    myRating.Username = user.UserName;
                    myRating.Rating = rating;
                    myRating.LastUpdated = DateTime.Now;
                    U_RatingRepository.Update(myRating);
                    U_RatingRepository.Save();

                    cRating.AverageRating = click.U_ClickRating.FirstOrDefault(d => d.ObjectID == objId).U_Rating.Average(d => d.Rating);
                    U_ClickRatingRepository.Save();
                }
                #endregion 更新用户评分
            }
            catch (Exception ex)
            {
                result.AppendMessage(ex);
            }

            result.success = string.IsNullOrEmpty(result.message);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
